#!/usr/bin/perl
# $Id$
#
# DESCRIPTION:
# Extracts usable translations from previously translated projects for
# terminology in Pootle and for automated simple translatations of .po files


use strict;

use constant DEFINITIONS => "../Makefile.in";

my ( $PRIORITY_FILE );
my ( $TRANSLATION_DIR, $TRANSLATION_EXT, $TERMINOLOGY_PO, $TERMINOLOGY_TERMS, $AUTOMATED_PO, $AUTOMATED_TERMS );
my ( $PROJECT, $PROJECT_EMAIL, $VERSION, $AUTHOR, $AUTHOR_EMAIL );

my %term;

print "Reading definitions file " . &DEFINITIONS . "\n";
open( INPUT_FILE, &DEFINITIONS ) || die $!;
while ( <INPUT_FILE> ) {

	chomp;

	/(.+) := (.+)/;

	my $key = $1;
	my $value = $2;

	if ( $key =~ /priority_file/i ) {
		$PRIORITY_FILE = $value;
	} elsif ( $key =~ /translation_dir/i ) {
		$TRANSLATION_DIR = $value;
	} elsif ( $key =~ /translation_ext/i ) {
		$TRANSLATION_EXT = $value;
	} elsif ( $key =~ /terminology_po_file/i ) {
		$TERMINOLOGY_PO = $value;
	} elsif ( $key =~ /terminology_terms_file/i ) {
		$TERMINOLOGY_TERMS = $value;
	} elsif ( $key =~ /automated_po_file/i ) {
		$AUTOMATED_PO = $value;
	} elsif ( $key =~ /automated_terms_file/i ) {
		$AUTOMATED_TERMS = $value;
	} elsif ( $key =~ /author_email/i ) {
		$AUTHOR_EMAIL = $value;
	} elsif ( $key =~ /author/i ) {
		$AUTHOR = $value;
	} elsif ( $key =~ /version/i ) {
		$VERSION = $value;
	} elsif ( $key =~ /project_email/i ) {
		$PROJECT_EMAIL = $value;
	} elsif ( $key =~ /project/i ) {
		$PROJECT = $value;
	}

}

close( INPUT_FILE );


sub lower($) {
	my ($word) = @_;
	$word = lc($word);
	$word =~ tr/ĀĒĪŌŪ/āēīōū/;
	return $word;
}


print "Reading " . $PRIORITY_FILE . "\n";
open( PRI_FILE, $PRIORITY_FILE ) || die $!;
print "Populating terms...\n";
while ( <PRI_FILE> ) {

	chomp;
	next unless /^[a-zA-Z]/;

	my $dir = $TRANSLATION_DIR . "/" . $_;
	print "  Processing dir $dir\n";
	opendir( INPUT_DIR, $dir ) || die $!;
	while ( my $file = readdir(INPUT_DIR) ) {

		chomp( $file );
		next unless $file =~ /$TRANSLATION_EXT/;

		print "    Processing file $file\n";
		open( INPUT_FILE, $dir . "/" . $file ) || die $!;
		while ( <INPUT_FILE> ) {

			# Skip if any html
			next if /</ || />/;

			# Skip known variables
			next if /\$a/;

			# Remove brackets and square brackets from the front and end of strings.
			s/^[\[\"\(](.*)[\]\"\)]\t/$1\t/;
			s/\t[\[\"\(](.*)[\]\")]$/\t$1/;

			# Remove traling characters
			s/[\.\?\!…]*\t/\t/;
			s/[\.\?\!…]*$//;

			# Remove starting commas
			s/^,//;
			s/\t,/\t/;

			# Chomp whitespace
			s/^\s*//;
			s/\t\s*/\t/;
			s/\s*\t/\t/;
			s/\s*$//;

			# Skip if less than 2 chars
			next unless /^[a-z][a-z]/i;

			/^([^\t]+)\t([^\t]+)/;
			$term{lower($1)} = lower($2);
		}
		close(INPUT_FILE);
	}
	closedir(INPUT_DIR);
}
close(PRI_FILE);
print "\n";


print "Writing terminology files...\n";

open( OUTPUT_AUTO_FILE, ">" . $AUTOMATED_TERMS ) || die $!;
open( OUTPUT_AUTO_PO_FILE, ">" . $AUTOMATED_PO ) || die $!;
open( OUTPUT_TERM_FILE, ">" . $TERMINOLOGY_TERMS ) || die $!;
open( OUTPUT_TERM_PO_FILE, ">" . $TERMINOLOGY_PO ) || die $!;

my $header =
'msgid ""
msgstr ""
"Project-Id-Version: ' . $VERSION . '\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2008-07-01 00:00:00+0000\n"
"PO-Revision-Date: ' . system( "date '+%Y-%m-%d %H:%M:%S%z'" ) . '\n"
"Last-Translator: ' . $AUTHOR . ' <' . $AUTHOR_EMAIL . '>\n"
"Language-Team: ' . $PROJECT . ' <' . $PROJECT_EMAIL . '>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"';

print OUTPUT_AUTO_PO_FILE $header . "\n";
print OUTPUT_TERM_PO_FILE $header . "\n";

foreach my $word (sort keys %term) {

	next if $word =~ /^$/;

	print OUTPUT_AUTO_FILE $word . "\t" . $term{$word} . "\n";
	print OUTPUT_AUTO_PO_FILE "\nmsgid \"" . $word . "\"";
	print OUTPUT_AUTO_PO_FILE "\nmsgstr \"" . $term{$word} . "\"\n";

	if ( $word !~ / / ) {
		print OUTPUT_TERM_FILE $word . "\t" . $term{$word} . "\n";
		print OUTPUT_TERM_PO_FILE "\nmsgid \"" . $word . "\"";
		print OUTPUT_TERM_PO_FILE "\nmsgstr \"" . $term{$word} . "\"\n";
	}
}

close( OUTPUT_TERM_PO_FILE );
close( OUTPUT_TERM_FILE );
close( OUTPUT_AUTO_PO_FILE );
close( OUTPUT_AUTO_FILE );
